package cn.imu.appdevexperiment2020.work02.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.work02.bean.ItemEntity;

public class ItemRecycleViewAdapter extends RecyclerView.Adapter<ItemRecycleViewAdapter.ViewHolder> {
    private Context context;
    private List<ItemEntity> list;
    private View inflater;

    public ItemRecycleViewAdapter(Context context, List<ItemEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //创建ViewHolder，返回每一项的布局
        inflater = LayoutInflater.from(context).inflate(R.layout.view_item_layout, parent, false);
        ViewHolder holder = new ViewHolder(inflater);


        holder.textView.setOnClickListener((view) -> {
            int position = holder.getAdapterPosition();

            Toast.makeText(view.getContext(), list.get(position).getName() + "RECYCLE ", Toast.LENGTH_LONG).show();
        });

        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull ItemRecycleViewAdapter.ViewHolder holder, int position) {
        holder.textView.setText(list.get(position).getName() + "RECYCLE ");
        //设置监听


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
        }
    }
}
